//
//  QuickRegisterPage.m
//  jianzhik
//
//  Created by 黄健文 on 2018/8/6.
//  Copyright © 2018年 WKWebView. All rights reserved.
//
#import "ZQUserAgreementPage.h"
#import "NSArray+Extension.h"
#import "QuickRegisterPage.h"
#import <TYAttributedLabel/TYAttributedLabel.h>
#import "UIColor+change.h"
@interface QuickRegisterPage ()<TYAttributedLabelDelegate>
@property (weak, nonatomic) IBOutlet UILabel *zhLabel;
@property (weak, nonatomic) IBOutlet UILabel *pwLabel;
@property (weak, nonatomic) IBOutlet UILabel *sureLabel;
@property (weak, nonatomic) IBOutlet UILabel *yqmLabel;
@property (weak, nonatomic) IBOutlet TYAttributedLabel *xyLabel;

@end

@implementation QuickRegisterPage

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    self.zhLabel.attributedText = [self attributeWithLabel:self.zhLabel];
    self.pwLabel.attributedText = [self attributeWithLabel:self.pwLabel];
    self.sureLabel.attributedText = [self attributeWithLabel:self.sureLabel];
    self.yqmLabel.attributedText = [self attributeWithLabel:self.yqmLabel];

    //用户使用协议
    NSString *text =@"我点击\"注册\"按钮,即表示您同意@用户使用协议";
    NSArray *textArray = [text componentsSeparatedByString:@"@"];
    self.xyLabel.backgroundColor = [UIColor clearColor];
    [textArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:textArray[idx]];
        if (idx == 1) {
            // 追加(添加到最后) button
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            [button setTitle:@"用户使用协议" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor stringToColor:TinColor] forState:UIControlStateNormal];
            [button.titleLabel setFont:[UIFont systemFontOfSize:15]];
            [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
            button.frame = CGRectMake(0, 0, 100, 15);
            [self.xyLabel appendView:button];
        } else{
            [attributedString addAttributeTextColor:[UIColor blackColor]];
            [attributedString addAttributeFont:[UIFont systemFontOfSize:13]];
            [self.xyLabel appendTextAttributedString:attributedString];
        }
    }];
}

- (void)buttonClicked
{
    ZQUserAgreementPage *vc = [ZQUserAgreementPage new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)TextRun atPoint:(CGPoint)point
{
    NSLog(@"textStorageClickedAtPoint");
    if ([TextRun isKindOfClass:[TYLinkTextStorage class]]) {
        
        id linkStr = ((TYLinkTextStorage*)TextRun).linkData;
        if ([linkStr isKindOfClass:[NSString class]]) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"点击提示" message:linkStr delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
        }
    }else if ([TextRun isKindOfClass:[TYImageStorage class]]) {
        TYImageStorage *imageStorage = (TYImageStorage *)TextRun;
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"点击提示" message:[NSString stringWithFormat:@"你点击了%@图片",imageStorage.imageName] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
    }
}



- (NSMutableAttributedString *)attributeWithLabel:(UILabel *)label{
    //设置字体两端对齐
    CGSize textSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine| NSStringDrawingUsesFontLeading  attributes:@{NSFontAttributeName :label.font} context:nil].size;
    CGFloat margin = (label.frame.size.width - textSize.width)/(label.text.length - 1);
    NSNumber *number = [NSNumber numberWithFloat:margin];
    
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:label.text];
    //字间距 :NSKernAttributeName
    [attribute addAttribute:NSKernAttributeName value:number range:NSMakeRange(0, label.text.length - 1)];
    return attribute;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
